package org.iteam.xlz.core.service.base;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.iteam.commons.utils.DateUtil;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.customerservice.CustomerServiceType;

public class ServiceTypeService extends BaseService<CustomerServiceType>{
	static ServiceTypeService instance;
	private ServiceTypeService(){}
	
	public static ServiceTypeService getInstance(){
		if(null==instance){
			instance=new ServiceTypeService();
		}
		return instance;
	}
	
	public String getparentid(String tid){
		String s="";
		if(null==tid){
			return "";
		}
		try {
			CustomerServiceType t=super.show("select r.* from customservicetype l left join customerservice r on l.cstid=r.cstid where 1=1 and l.csid=?",tid);
			if(StringUtils.isNotBlank(t.getParentid())){
				s=t.getParentid();
			}
			return s;
		} catch (SQLException e) {
			e.printStackTrace();
			return s;
		}
	}
	
	public  List<TicketTree> getAllTree(){
		try {
			List<CustomerServiceType> all=super.query("select * from customservicetype");
			List<TicketTree> list=new ArrayList<TicketTree>();
			
			for(CustomerServiceType a:all){
				TicketTree at=new TicketTree();
				at.setId(a.getCstid());
				at.setText(a.getTypename());
				list.add(at);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<TicketTree>();
		}
	}
	
	public List<TicketTree> buildTicketTree(){
		try {
			List<CustomerServiceType> all=super.query("select * from customservicetype");
			List<TicketTree> list=new ArrayList<TicketTree>();
			List<TicketTree> secondList=new ArrayList<TicketTree>();
			
			for(CustomerServiceType a:all){//设置一级区域
				if(null==a.getParentid()){
					TicketTree at=new TicketTree();
					at.setId(a.getCstid());
					at.setText(a.getTypename());
					list.add(at);
				}
			}
			
			for(TicketTree at:list){//设置二级区域
				List<TicketTree> children=new ArrayList<TicketTree>();
				for(CustomerServiceType a:all){
					if(at.getId().equals(a.getParentid())){
						TicketTree ati=new TicketTree();
						ati.setId(a.getCstid());
						ati.setText(a.getTypename());
						children.add(ati);
						secondList.add(ati);//这里面保存的全是二级区域
					}
				}
				at.setChildren(children);
			}
			
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<TicketTree>();
		}
	}
	
	public long counts(String parentid){
		StringBuilder sql=new StringBuilder("select count(1) from customservicetype where 1=1 ");
		List<Object> params=new ArrayList<Object>();
		if(StringUtils.isNotBlank(parentid)){
			sql.append(" and parentid=?");
			params.add(parentid);
		}else{
			sql.append(" and parentid is null");
		}
		try {
			return super.counts(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<CustomerServiceType> query(String parentid,long page,long rows){
		StringBuilder sql=new StringBuilder("select * from customservicetype where 1=1 ");
		List<Object> params=new ArrayList<Object>();
		if(StringUtils.isNotBlank(parentid)){
			sql.append(" and parentid=?");
			params.add(parentid);
		}else{
			sql.append(" and parentid is null");
		}
		sql.append(" limit ?,?");
		params.add((page-1)*rows);
		params.add(rows);
		try {
			return super.query(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<CustomerServiceType>();
		}
	}
	
	public boolean create(CustomerServiceType model){
		SqlBuilder sql=SqlBuilder.createInsert("customservicetype");
		sql.addField("cstid",model.getCstid());
		sql.addField("typename",model.getTypename());
		if(StringUtils.isNotBlank(model.getParentid())){
			sql.addField("parentid",model.getParentid());
		}
		sql.addField("createtime",DateUtil.currentTime());
		sql.addField("creator",model.getCreator());
		sql.addField("status",model.getStatus());
		try {
			return super.updates(sql.getSql(),sql.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean update(CustomerServiceType model){
		SqlBuilder sql=SqlBuilder.createUpdate("customservicetype");
		sql.addField("typename",model.getTypename());
		sql.addField("status",model.getStatus());
		sql.addWhere("cstid",model.getCstid());
		try {
			return super.updates(sql.getSql(),sql.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean delete(String cstid){
		try {
			return super.updates("delete from customservicetype where 1=1 and cstid=?",cstid)>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public CustomerServiceType findById(String cstid){
		try {
			return super.show("select * from customservicetype where 1=1 and cstid=?",cstid);
		} catch (SQLException e) {
			e.printStackTrace();
			return new CustomerServiceType();
		}
	}
	
	public class TicketTree{
		String id;
		String text;
		List<TicketTree> children;
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public String getText() {
			return text;
		}
		public void setText(String text) {
			this.text = text;
		}
		public List<TicketTree> getChildren() {
			return children;
		}
		public void setChildren(List<TicketTree> children) {
			this.children = children;
		}
		
	}	
}
